<?php

Yii::import( "ext.xupload.models.XUploadForm" );
Yii::import( "ext.xupload.models.ImageUploadForm" );
Yii::import( "application.controllers.FrontendBaseController" );

class ContestController extends FrontendBaseController
{
    public function actionIndex()
    {
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Конкурсы"          => "");
        
        $contests = ContestFacade::getActualContests();
        $count = ContestFacade::getActualContestsCount();
        
        $this->render("contest", array(
            "contests"  => $contests,
            "count"     => $count
        ));
    }
    
    public function actionAdd()
    {                
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Конкурсы"          => "/contest",
                                    "Создание конкурса" => "" );
        
        if (!isset($_POST['Contest']))
        {
            $model = new Contest();
        
            $this->render('add', array('model' => $model));
        }
        else
        {            
            $contest = ContestFacade::createContest($_POST['Contest']);
            
            if ($contest->hasErrors())
            {
                $this->render('add', array('model' => $contest));
            }
            else
            {
                $this->redirect('/contest/view/' . $contest->id);
            }
        }
    }
    
    public function actionSetImage( $id )
    {
        $model = $this->loadModel( $id );        
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Конкурсы" => "/contest",
                                    $model->name => "/contest/view/" . $id,
                                    "Загрузка изображения" => "");                        
        
        $this->render('setimage', array(
            'model' => $model
        ));
    }
    
    public function actionAddPhoto( $id )
    {
        $model = $this->loadModel($id);
        if (Yii::app()->user->isGuest)
        {
            $this->redirect("/contest/view/" . $id);
        }                
        $contestImage = ContestFacade::getContestImageByContest($id);                
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Конкурсы" => "/contest",
                                    $model->name => "/contest/view/" . $id,
                                    "Загрузка изображения" => "");
        
        $this->render('addphoto', array(
            'model'         => $model,
            'contestImage'  => $contestImage
        ));
    }
    
    public function actionView( $id )
    {
        $model = $this->loadModel($id);
        
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Конкурсы"          => "/contest",
                                    $model->name => "" );
        
        $this->render('view', array(
            'model' => $model
        ));
    }
    
    public function actionParticipants( $id )
    {
        $model = $this->loadModel($id);
        
        $this->breadcrumbs = array( "Главная"           => "/",
                                    "Моя страница"      => $this->getUserMainPageUrl(),
                                    "Конкурсы"          => "/contest",
                                    $model->name        => "/contest/view/" . $model->id,
                                    "Участники"         => '');
        
        $images = ContestFacade::getContestImages($id);
        $countImages = ContestFacade::getContestImagesCount($id);
        
        $this->render('participants', array(
            'model'         => $model,
            'images'        => $images,
            'countImages'   => $countImages
        ));
    }
    
    public function actionImage( $id )
    {
        $contestImage = ContestFacade::getContestImageById($id);
        $model = $this->loadModel($contestImage->contest_id);
        
        $this->breadcrumbs = array( "Главная"               => "/",
                                    "Моя страница"          => $this->getUserMainPageUrl(),
                                    "Конкурсы"              => "/contest",
                                    $model->name            => "/contest/view/" . $model->id,
                                    "Фотографии"            => "/contest/participants/" . $model->id,
                                    "Просмотр фотографии"   => '');
                
        $author = UserFacade::getUserById($contestImage->user_id);        
        
        $this->render("image", array(
            "image"     => $contestImage,        
            "author"    => $author
        ));
    }
    
    public function actionDelete()
    {
        
    }
    
    public function actionVote( $id )
    {
        $model = ContestFacade::getContestImageById($id);
                
        if (!Yii::app()->user->isGuest && !ContestFacade::isAlreadyVote($id, $this->getUserIdFromSession()))
        {
            ContestFacade::setVote($id, $this->getUserIdFromSession());            
        }
        
        $this->redirect("/contest/participants/" . $model->contest_id);
    }
    
    private function loadModel($id)
    {
        return ContestFacade::loadModel($id);
    }    
}
?>
